package org.cloudie.docs.editor.client.widgets;

import org.cloudie.docs.editor.client.behaviors.HasProxyBehavior;
import org.cloudie.docs.editor.client.commands.Command;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.events.DoubleClickHandler;

import com.google.gwt.user.client.ui.Widget;

import com.google.gwt.event.dom.client.ClickEvent;

public class ExplorerIcon extends Icon implements HasProxyBehavior {

  protected boolean isMovable = true, isProxy = false;
  
  public ExplorerIcon(String id, String label, String image,
      String tooltip, Command cmd) {
	super(id, label, image, true, tooltip, false, cmd);
    this.container.setStylePrimaryName("cloudie-explorer-icon");
    this.container.addClickHandler(new DoubleClickHandler() {
      @Override
      public void onDoubleClick(ClickEvent event) {
        if (command != null) {
          CommandEvent.fire(ExplorerIcon.this.command);
        }
      }
    });
  }
  
  @Override
  public Widget getDragHandle() {
    return container;
  }

  @Override
  public boolean allowsDropping(Widget receiver) {
	if (!isMovable) return false;
	return true;
  }

  @Override
  public boolean allowsDropping() {
	if (!isMovable) return false;
	return true;
  }
  
  @Override
  public void onDropCanceled() {
	if (isProxy) {
	  this.removeFromParent();
	}
  }

  @Override
  public void onDropped(Widget receiver, int x, int y) {
    if (isProxy) {
	  this.removeFromParent();
	}
  }

  @Override
  public HasProxyBehavior getProxy() {
	ExplorerIcon proxy = new ExplorerIcon(getId(), getLabel(), getImage(),
	    getTooltip(), getCommand());
	proxy.isProxy = true;
	return proxy;
  }

  @Override
  public boolean isProxy() {
    return isProxy;
  }

  @Override
  public void setProxy(boolean isProxy) {
    this.isProxy = isProxy;
  }

  public boolean isMovable() {
    return isMovable;
  }

  public void setMovable(boolean isMovable) {
    this.isMovable = isMovable;
  }

}
